<?php

 Class CommentModel extends Model{

     static $pageLine = 10;

     /**
      * 获得单条数据
      * @param $condition 条件
      * @return mixed
      */
     public function getCommentByOne($condition,$field){
         return $this->where($condition)->field($field)->find();
     }

     /**
      * 获得多条数据
      * @param $condition 条件
      * @param $field 字段
      * @param $order 排序
      * @param $limit 数量
      * @return mixed
      */
     public function getCommentByMore($condition,$field,$order,$limit){
         return $this->where($condition)->field($field)->order($order)->limit($limit)->select();
     }

     /**
      * @param $data 数据
      * @return mixed|void
      */
     public function insert($data){
         if(empty($data)){
             return;
         }
         return $this->add($data);
     }

     /**
      * 更新数据
      */
     public function updata($condition,$data){
         return $this->where($condition)->save($data);
     }

     /**
      * 获取评论
      */
     public function getCommentList($condition,$isCUser,$page=1){
           $count = $this->where($condition)->count();
           $page = $page?$page:1;
           $starmit = ($page-1)*self::$pageLine;
           $data = $this->getCommentByMore($condition,'','',$starmit.",".self::$pageLine);
           if($isCUser){
                 foreach($data as $k => $v){
                       $where['id'] = $v['uid'];
                       $data[$k]['user'] = D('Member')->getMemberByOne($where);
                 }
           }
           // 组合评论
           $comment['data'] = $this->UnlimitByReplay($data);
           $comment['count'] = $count;
           return $comment;
     }

     /**
      * 组合评论
      */
     public function UnlimitByReplay($data){
         $tem_p = $data;
         $arr = [];
         foreach($data as $v){
             $is = true;
             foreach($tem_p as $k => $value){
                 if($v['rid']==$value['id']) {
                     $arr[$k]['child'][] = $v;
                     $is = false;
                 }
             }
             if($is) {
                 $arr[] = $v;
             }
         }
         return $arr;
     }

     /**
      * 添加评论处理
      * @param $data 数据
      * @param $isRep 是否回复
      * @return array
      */
     public function CommentAdd($data,$isRep){

          // 数据检测 //过滤
          $comment = htmlspecialchars($data['content']);
          $id = intval($data['postid']);

          if(empty($comment)&&empty($id)){
              $res = ['status'=>400,'msg'=>'参数错误'];
              return $res;
          }

          if(!session('uid')){
              $res = ['status'=>400,'msg'=>'未登录'];
              return $res;
          }

           // 判断该文章id 是否存在
           $awhere['id'] = $id;
           $is_has = D("article")->getArticleByOne($awhere);

           if(!$is_has) {
                 $res = ['status'=>400,'msg'=>'该文章不存在'];
                 return $res;
           }

         /**
          * 判断是否为回复评论
          */
           if($isRep){
                $rid = $_POST['comid'];
           }else{
                $rid = 0;
           }

           $data['comment'] = $comment;
           $data['uid'] = session('uid');
           $data['pid'] = $id;
           $data['rid'] = $rid;
           $data['created_at'] = time();
           // 插入数据
           $inserid = $this->insert($data);
           if($inserid){
                  D("article")->setFiledInc(array('id'=>$id),'comment');
                  $member = D('Member')->getMemberByOne(array('id'=>$data['uid']));
                  $data['avator'] = $member['face'];
                  $data['name'] = $member['name'];
                  $data['date'] = dateFormat(time());
                  $data['id'] = $inserid;
                  $res = ['status'=>200,'msg'=>'添加评论成功','data'=>$data];
                  return $res;
           }else{
                  $res = ['status'=>400,'msg'=>'添加评论失败'];
                  return $res;
           }
     }

 }